package site.shader85.Kingdoms.domain.area;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;

import com.google.common.collect.Maps;

@SerializableAs("Position")
public class Position implements ConfigurationSerializable{

	public final int x;
	public final int y;
	public final int z;

	private Position(int x, int y, int z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public static Position byLocation(Location loc) {
		int x = loc.getBlockX();
		int y = loc.getBlockY();
		int z = loc.getBlockZ();
		return new Position(x, y, z);
	}
	
	public Location toLocation(World w){
		return new Location(w,x,y,z);
	}
	
	public static Position getPosition(int x, int y, int z) {
		return new Position(x, y, z);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Position) {
			Position pos = (Position) obj;
			if (pos.x == x && pos.y == y && pos.z == z) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode(){
		String s = ((""+x)+y)+z;
		return s.hashCode();
	}

	@Override
	public Map<String, Object> serialize() {
		HashMap<String, Object> table = Maps.newHashMap();
		table.put("x", x);
		table.put("y", y);
		table.put("z", z);
		return table;
	}
	
	public static Position deserialize(Map<String, Object> table) {
		int x = ((Integer)table.get("x")).intValue();
		int y = ((Integer)table.get("y")).intValue();
		int z = ((Integer)table.get("z")).intValue();
		return new Position(x,y,z);
	}
}
